<li><a class="reference internal" href="#general-information" id="id1">1 General Information</a><ul class="auto-toc">
<li><a class="reference internal" href="#what-is-python" id="id2">1.1 What is Python?</a></li>
<li><a class="reference internal" href="#why-was-python-created-in-the-first-place" id="id3">1.2 Why was Python created in the first place?</a></li>
<li><a class="reference internal" href="#what-is-python-good-for" id="id4">1.3 What is Python good for?</a></li>
<li><a class="reference internal" href="#how-does-the-python-version-numbering-scheme-work" id="id5">1.4 How does the Python version numbering scheme work?</a></li>
<li><a class="reference internal" href="#are-there-copyright-restrictions-on-the-use-of-python" id="id6">1.5 Are there copyright restrictions on the use of Python?</a></li>
<li><a class="reference internal" href="#how-do-i-obtain-a-copy-of-the-python-source" id="id7">1.6 How do I obtain a copy of the Python source?</a></li>
<li><a class="reference internal" href="#how-do-i-get-documentation-on-python" id="id8">1.7 How do I get documentation on Python?</a></li>
<li><a class="reference internal" href="#i-ve-never-programmed-before-is-there-a-python-tutorial" id="id9">1.8 I've never programmed before. Is there a Python tutorial?</a></li>
<li><a class="reference internal" href="#are-there-other-ftp-sites-that-mirror-the-python-distribution" id="id10">1.9 Are there other FTP sites that mirror the Python distribution?</a></li>
<li><a class="reference internal" href="#is-there-a-newsgroup-or-mailing-list-devoted-to-python" id="id11">1.10 Is there a newsgroup or mailing list devoted to Python?</a></li>
<li><a class="reference internal" href="#how-do-i-get-a-beta-test-version-of-python" id="id12">1.11 How do I get a beta test version of Python?</a></li>
<li><a class="reference internal" href="#how-do-i-submit-bug-reports-and-patches-for-python" id="id13">1.12 How do I submit bug reports and patches for Python?</a></li>
<li><a class="reference internal" href="#are-there-any-published-articles-about-python-that-i-can-reference" id="id14">1.13 Are there any published articles about Python that I can reference?</a></li>
<li><a class="reference internal" href="#are-there-any-books-on-python" id="id15">1.14 Are there any books on Python?</a></li>
<li><a class="reference internal" href="#where-in-the-world-is-www-python-org-located" id="id16">1.15 Where in the world is www.python.org located?</a></li>
<li><a class="reference internal" href="#why-is-it-called-python" id="id17">1.16 Why is it called Python?</a></li>
<li><a class="reference internal" href="#do-i-have-to-like-monty-python-s-flying-circus" id="id18">1.17 Do I have to like "Monty Python's Flying Circus"?</a></li>
</ul>
</li>
<li><a class="reference internal" href="#python-in-the-real-world" id="id19">2 Python in the real world</a><ul class="auto-toc">
<li><a class="reference internal" href="#how-stable-is-python" id="id20">2.1 How stable is Python?</a></li>
<li><a class="reference internal" href="#how-many-people-are-using-python" id="id21">2.2 How many people are using Python?</a></li>
<li><a class="reference internal" href="#have-any-significant-projects-been-done-in-python" id="id22">2.3 Have any significant projects been done in Python?</a></li>
<li><a class="reference internal" href="#what-new-developments-are-expected-for-python-in-the-future" id="id23">2.4 What new developments are expected for Python in the future?</a></li>
<li><a class="reference internal" href="#is-it-reasonable-to-propose-incompatible-changes-to-python" id="id24">2.5 Is it reasonable to propose incompatible changes to Python?</a></li>
<li><a class="reference internal" href="#what-is-the-python-software-foundation" id="id25">2.6 What is the Python Software Foundation?</a></li>
<li><a class="reference internal" href="#is-python-a-good-language-for-beginning-programmers" id="id27">2.8 Is Python a good language for beginning programmers?</a></li>
<li><a class="reference internal" href="#what-is-this-bsddb185-module-my-application-keeps-complaining-about" id="id29">3.1 What is this bsddb185 module my application keeps complaining about?</a></li>
<li><a class="reference internal" href="#why-does-python-use-indentation-for-grouping-of-statements" id="id31">4.1 Why does Python use indentation for grouping of statements?</a></li>
<li><a class="reference internal" href="#why-am-i-getting-strange-results-with-simple-arithmetic-operations" id="id32">4.2 Why am I getting strange results with simple arithmetic operations?</a></li>
<li><a class="reference internal" href="#why-are-floating-point-calculations-so-inaccurate" id="id33">4.3 Why are floating point calculations so inaccurate?</a></li>
<li><a class="reference internal" href="#why-are-python-strings-immutable" id="id34">4.4 Why are Python strings immutable?</a></li>
<li><a class="reference internal" href="#why-must-self-be-used-explicitly-in-method-definitions-and-calls" id="id35">4.5 Why must 'self' be used explicitly in method definitions and calls?</a></li>
<li><a class="reference internal" href="#why-can-t-i-use-an-assignment-in-an-expression" id="id36">4.6 Why can't I use an assignment in an expression?</a></li>
<li><a class="reference internal" href="#why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list" id="id37">4.7 Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</a></li>
<li><a class="reference internal" href="#why-is-join-a-string-method-instead-of-a-list-or-tuple-method" id="id38">4.8 Why is join() a string method instead of a list or tuple method?</a></li>
<li><a class="reference internal" href="#how-fast-are-exceptions" id="id39">4.9 How fast are exceptions?</a></li>
<li><a class="reference internal" href="#why-isn-t-there-a-switch-or-case-statement-in-python" id="id40">4.10 Why isn't there a switch or case statement in Python?</a></li>
<li><a class="reference internal" href="#can-t-you-emulate-threads-in-the-interpreter-instead-of-relying-on-an-os-specific-thread-implementation" id="id41">4.11 Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</a></li>
<li><a class="reference internal" href="#can-python-be-compiled-to-machine-code-c-or-some-other-language" id="id43">4.13 Can Python be compiled to machine code, C or some other language?</a></li>
<li><a class="reference internal" href="#how-does-python-manage-memory" id="id44">4.14 How does Python manage memory?</a></li>
<li><a class="reference internal" href="#why-isn-t-all-memory-freed-when-python-exits" id="id45">4.15 Why isn't all memory freed when Python exits?</a></li>
<li><a class="reference internal" href="#why-are-there-separate-tuple-and-list-data-types" id="id46">4.16 Why are there separate tuple and list data types?</a></li>
<li><a class="reference internal" href="#how-are-lists-implemented" id="id47">4.17 How are lists implemented?</a></li>
<li><a class="reference internal" href="#how-are-dictionaries-implemented" id="id48">4.18 How are dictionaries implemented?</a></li>
<li><a class="reference internal" href="#why-must-dictionary-keys-be-immutable" id="id49">4.19 Why must dictionary keys be immutable?</a></li>
<li><a class="reference internal" href="#how-do-you-specify-and-enforce-an-interface-spec-in-python" id="id51">4.21 How do you specify and enforce an interface spec in Python?</a></li>
<li><a class="reference internal" href="#why-are-default-values-shared-between-objects" id="id52">4.22 Why are default values shared between objects?</a></li>
<li><a class="reference internal" href="#why-is-there-no-goto" id="id53">4.23 Why is there no goto?</a></li>
<li><a class="reference internal" href="#why-do-i-get-a-syntaxerror-for-a-continue-inside-a-try" id="id54">4.24 Why do I get a SyntaxError for a 'continue' inside a 'try'?</a></li>
<li><a class="reference internal" href="#why-can-t-raw-strings-r-strings-end-with-a-backslash" id="id55">4.25 Why can't raw strings (r-strings) end with a backslash?</a></li>
<li><a class="reference internal" href="#why-doesn-t-python-have-a-with-statement-like-some-other-languages" id="id56">4.26 Why doesn't Python have a "with" statement like some other languages?</a></li>
<li><a class="reference internal" href="#why-are-colons-required-for-the-if-while-def-class-statements" id="id57">4.27 Why are colons required for the if/while/def/class statements?</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="general-information">
<h1><a class="toc-backref" href="#id1">1 General Information</a></h1>
<div class="section" id="what-is-python">
<h2><a class="toc-backref" href="#id2">1.1 What is Python?</a></h2>
<p>Python is an interpreted, interactive, object-oriented programming
language. It incorporates modules, exceptions, dynamic typing, very
high level dynamic data types, and classes. Python combines
remarkable power with very clear syntax. It has interfaces to many
system calls and libraries, as well as to various window systems, and
is extensible in C or C++. It is also usable as an extension language
for applications that need a programmable interface. Finally, Python
is portable: it runs on many Unix variants, on the Mac, and on PCs
under MS-DOS, Windows, Windows NT, and OS/2.</p>
<p>To find out more, start
with the <a class="reference external" href="http://www.python.org/moin/BeginnersGuide">Beginner's Guide to Python</a>.</p>
<h2><a class="toc-backref" href="#id5">1.4 How does the Python version numbering scheme work?</a></h2>
<p>Python versions are numbered A.B.C or A.B. A is the major version
number -- it is only incremented for really major changes in
the language. B is the minor version number, incremented for less
earth-shattering changes. C is the micro-level -- it is incremented
for each bugfix release. See <a class="reference external" href="../../peps/pep-0006.html">PEP 6</a>
for more information about bugfix releases.</p>
<p>Not all releases are bugfix releases. In the run-up to a new major
release, a series of development releases are made, denoted as alpha,
beta, or release candidate. Alphas are early releases in which
interfaces aren't yet finalized; it's not unexpected to see an
interface change between two alpha releases. Betas are more stable,
preserving existing interfaces but possibly adding new modules, and
release candidates are frozen, making no changes except as needed to
fix critical bugs.</p>
<p>Alpha, beta and release candidate versions have an additional
suffix. The suffix for an alpha version is "aN" for some small
number N, the suffix for a beta version is "bN" for some small number
N, and the suffix for a release candidate version is "cN" for some
small number N. In other words, all versions labeled 2.0aN precede
the versions labeled 2.0bN, which precede versions labeled 2.0cN, and
<em>those</em> precede 2.0.</p>
<p>You may also find version numbers with a "+" suffix, e.g. "2.2+".
These are unreleased versions, built directly from the subversion trunk.
In practice, after a final minor release is made, the subversion trunk is
incremented to the next minor version, which becomes the "a0" version,
e.g. "2.4a0".</p>
<p>See also the documentation for <tt class="docutils literal"><span class="pre">sys.version</span></tt>, <tt class="docutils literal"><span class="pre">sys.hexversion</span></tt>, and
<h2><a class="toc-backref" href="#id10">1.9 Are there other FTP sites that mirror the Python distribution?</a></h2>
<p>Consult the list of python.org mirrors at <a class="reference external" href="http://www.python.org/Mirrors.html">http://www.python.org/Mirrors.html</a>.</p>
<h2><a class="toc-backref" href="#id12">1.11 How do I get a beta test version of Python?</a></h2>
<p>All releases, including alphas, betas and release candidates, are
announced on the comp.lang.python and comp.lang.python.announce
newsgroups. All announcements also appear on the Python
home page, at <a class="reference external" href="http://www.python.org/">http://www.python.org/</a>; an RSS feed of news is available.</p>
<p>You can also access the development version of Python through subversion.
See <a class="reference external" href="http://www.python.org/dev/devfaq.html#subversion-svn">http://www.python.org/dev/devfaq.html#subversion-svn</a> for details.</p>
<h2><a class="toc-backref" href="#id15">1.14 Are there any books on Python?</a></h2>
<p>Yes, there are many, and more are being published. See
the python.org Wiki at <a class="reference external" href="http://www.python.org/moin/PythonBooks">http://www.python.org/moin/PythonBooks</a> for a list.</p>
<p>You can also search online bookstores for "Python"
<h2><a class="toc-backref" href="#id16">1.15 Where in the world is www.python.org located?</a></h2>
<p>It's currently in Amsterdam, graciously hosted by <a class="reference external" href="http://www.xs4all.nl">XS4ALL</a>. Thanks to Thomas Wouters for his work in
<h2><a class="toc-backref" href="#id22">2.3 Have any significant projects been done in Python?</a></h2>
<p>See <a class="reference external" href="http://www.pythonology.org/success">http://www.pythonology.org/success</a> for a list of projects that
use Python. Consulting the proceedings for <a class="reference external" href="../../workshops/">past Python conferences</a> will reveal contributions from
many different companies and organizations.</p>
<p>High-profile Python projects include <a class="reference external" href="http://www.list.org">the Mailman mailing list manager</a> and <a class="reference external" href="http://www.zope.org">the Zope application server</a>. Several Linux distributions, most
notably <a class="reference external" href="http://www.redhat.com">Red Hat</a>, have written part or all
of their installer and system administration software in Python. Companies
<h2><a class="toc-backref" href="#id26">2.7 Is Python Y2K (Year 2000) Compliant?</a></h2>
<p>As of August, 2003 no major problems have been reported and Y2K
compliance seems to be a non-issue.</p>
<p>Python does very few date calculations and for those it does perform relies
on the C library functions. Python generally represents times either
as seconds since 1970 or as a <tt class="docutils literal"><span class="pre">(year,</span> <span class="pre">month,</span> <span class="pre">day,</span> <span class="pre">...)</span></tt> tuple where the
year is expressed with four digits, which makes Y2K bugs unlikely. So
as long as your C library is okay, Python should be okay. Of course,
it's possible that a particular application written in Python
makes assumptions about 2-digit years.</p>
<p>Because Python is available free of charge, there are no absolute
guarantees. If there <em>are</em> unforseen problems, liability is the
user's problem rather than the developers', and there is nobody you can sue
for damages. The Python copyright notice contains the following
disclaimer:</p>
<blockquote>
<p>4. PSF is making Python 2.3 available to Licensee on an "AS IS"
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.3 WILL NOT
INFRINGE ANY THIRD PARTY RIGHTS.</p>
<p>5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
2.3 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.3,
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.</p>
</blockquote>
<p>The good news is that <em>if</em> you encounter a problem, you have full
source available to track it down and fix it. This is one advantage of
<h2><a class="toc-backref" href="#id29">3.1 What is this bsddb185 module my application keeps complaining about?</a></h2>
<p>Starting with Python2.3, the distribution includes the <cite>PyBSDDB package
<http://pybsddb.sf.net/></cite> as a replacement for the old bsddb module. It
includes functions which provide backward compatibility at the API level,
but requires a newer version of the underlying <a class="reference external" href="http://www.sleepycat.com">Berkeley DB</a> library. Files created with the older bsddb
module can't be opened directly using the new module.</p>
<p>Using your old version of Python and a pair of scripts which are part of
Python 2.3 (db2pickle.py and pickle2db.py, in the Tools/scripts directory)
you can convert your old database files to the new format. Using your old
Python version, run the db2pickle.py script to convert it to a pickle,
<h2><a class="toc-backref" href="#id33">4.3 Why are floating point calculations so inaccurate?</a></h2>
<p>People are often very surprised by results like this:</p>
<pre class="literal-block">
>>> 1.2-1.0
0.199999999999999996
</pre>
<p>and think it is a bug in Python. It's not. It's a problem caused by
the internal representation of floating point numbers, which uses a
fixed number of binary digits to represent a decimal number. Some
decimal numbers can't be represented exactly in binary, resulting in
small roundoff errors.</p>
<p>In decimal math, there are many numbers that can't be represented with a
fixed number of decimal digits, e.g. 1/3 = 0.3333333333.......</p>
<p>In base 2, 1/2 = 0.1, 1/4 = 0.01, 1/8 = 0.001, etc.
.2 equals 2/10 equals 1/5, resulting in the binary fractional number
0.001100110011001...</p>
<p>Floating point numbers only have 32 or 64 bits of precision, so the digits are cut off at some point,
and the resulting number is 0.199999999999999996 in decimal, not 0.2.</p>
<p>A floating point's <tt class="docutils literal"><span class="pre">repr()</span></tt> function prints as many digits are
necessary to make <tt class="docutils literal"><span class="pre">eval(repr(f))</span> <span class="pre">==</span> <span class="pre">f</span></tt> true for any float f. The
<tt class="docutils literal"><span class="pre">str()</span></tt> function prints fewer digits and this often results in the
more sensible number that was probably intended:</p>
<pre class="literal-block">
>>> 0.2
0.20000000000000001
>>> print 0.2
0.2
</pre>
<p>Again, this has nothing to do with Python, but with the way the underlying C
platform handles floating point numbers, and ultimately with the inaccuracy
you'll always have when writing down numbers as a string of a fixed number
of digits.</p>
<p>One of the consequences of this is that it is dangerous to compare the
result of some computation to a float with == ! Tiny inaccuracies may mean
that == fails.
Instead, you have to check that the difference between the
two numbers is less than a certain threshold:</p>
<pre class="literal-block">
epsilon = 0.0000000000001 # Tiny allowed error
expected_result = 0.4
if expected_result-epsilon <= computation() <= expected_result+epsilon:
...
</pre>
<p>Please see the chapter on
<a class="reference external" href="http://docs.python.org/tut/node16.html">floating point arithmetic</a>
<h2><a class="toc-backref" href="#id35">4.5 Why must 'self' be used explicitly in method definitions and calls?</a></h2>
<p>The idea was borrowed from Modula-3. It turns out to be very useful,
for a variety of reasons.</p>
<p>First, it's more obvious that you are using a method or instance
attribute instead of a local variable. Reading <tt class="docutils literal"><span class="pre">self.x</span></tt> or <tt class="docutils literal"><span class="pre">self.meth()</span></tt>
makes it absolutely clear that an instance variable or method is used even
if you don't know the class definition by heart. In C++, you can sort of
tell by the lack of a local variable declaration (assuming globals are rare
or easily recognizable) -- but in Python, there are no local variable
declarations, so you'd have to look up the class definition to be sure.
Some C++ and Java coding standards call for instance attributes to have an
<tt class="docutils literal"><span class="pre">m_</span></tt> prefix, so this explicitness is still useful in those languages, too.</p>
<p>Second, it means that no special syntax is necessary if you want to
explicitly reference or call the method from a particular class. In C++, if
you want to use a method from a base class which is overridden in a derived
class, you have to use the :: operator -- in Python you can write
baseclass.methodname(self, <argument list>). This is particularly useful
for __init__() methods, and in general in cases where a derived class method
wants to extend the base class method of the same name and thus has to call
the base class method somehow.</p>
<p>Finally, for instance variables it solves a syntactic problem with
assignment: since local variables in Python are (by definition!) those
variables to which a value assigned in a function body (and that aren't
explicitly declared global), there has to be some way to tell the
interpreter that an assignment was meant to assign to an instance variable
instead of to a local variable, and it should preferably be syntactic (for
efficiency reasons). C++ does this through declarations, but Python doesn't
have declarations and it would be a pity having to introduce them just for
this purpose. Using the explicit "self.var" solves this nicely. Similarly,
for using instance variables, having to write "self.var" means that
references to unqualified names inside a method don't have to search the
instance's directories. To put it another way, local variables and
instance variables live in two different namespaces, and you need to
<h2><a class="toc-backref" href="#id36">4.6 Why can't I use an assignment in an expression?</a></h2>
<p>Many people used to C or Perl complain that they want to
use this C idiom:</p>
<pre class="literal-block">
while (line = readline(f)) {
...do something with line...
}
</pre>
<p>where in Python you're forced to write this:</p>
<pre class="literal-block">
while True:
line = f.readline()
if not line:
break
...do something with line...
</pre>
<p>The reason for not allowing assignment in Python expressions
is a common, hard-to-find bug in those other languages,
caused by this construct:</p>
<pre class="literal-block">
if (x = 0) {
...error handling...
}
else {
...code that only works for nonzero x...
}
</pre>
<p>The error is a simple typo: <tt class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">0</span></tt>, which assigns 0 to the variable <tt class="docutils literal"><span class="pre">x</span></tt>,
was written while the comparison <tt class="docutils literal"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">0</span></tt> is certainly what was intended.</p>
<p>Many alternatives have been proposed. Most are hacks that save some
typing but use arbitrary or cryptic syntax or keywords,
and fail the simple criterion for language change proposals:
it should intuitively suggest the proper meaning to a human reader
who has not yet been introduced to the construct.</p>
<p>An interesting phenomenon is that most experienced Python programmers
recognize the "while True" idiom and don't seem to be missing the
assignment in expression construct much; it's only newcomers
who express a strong desire to add this to the language.</p>
<p>There's an alternative way of spelling this that seems
attractive but is generally less robust than the "while True" solution:</p>
<pre class="literal-block">
line = f.readline()
while line:
...do something with line...
line = f.readline()
</pre>
<p>The problem with this is that if you change your mind about exactly
how you get the next line (e.g. you want to change it into
<tt class="docutils literal"><span class="pre">sys.stdin.readline()</span></tt>) you have to remember to change two places in
your program -- the second occurrence is hidden at the bottom of the
loop.</p>
<p>The best approach is to use iterators, making it possible to loop
through objects using the <tt class="docutils literal"><span class="pre">for</span></tt> statement. For example, in the
current version of Python file objects support the iterator protocol, so you
<h2><a class="toc-backref" href="#id37">4.7 Why does Python use methods for some functionality (e.g. list.index()) but functions for other (e.g. len(list))?</a></h2>
<p>The major reason is history. Functions were used for those operations
that were generic for a group of types and which were intended to work
even for objects that didn't have methods at all (e.g. tuples). It is
also convenient to have a function that can readily be applied to an
amorphous collection of objects when you use the functional features
of Python (<tt class="docutils literal"><span class="pre">map()</span></tt>, <tt class="docutils literal"><span class="pre">apply()</span></tt> et al).</p>
<p>In fact, implementing <tt class="docutils literal"><span class="pre">len()</span></tt>, <tt class="docutils literal"><span class="pre">max()</span></tt>, <tt class="docutils literal"><span class="pre">min()</span></tt> as a built-in
function is actually less code than implementing them as methods for
each type. One can quibble about individual cases but it's a part of
Python, and it's too late to make such fundamental changes now. The
functions have to remain to avoid massive code breakage.</p>
<p>Note that for string operations Python has moved from external functions
(the <tt class="docutils literal"><span class="pre">string</span></tt> module) to methods. However, <tt class="docutils literal"><span class="pre">len()</span></tt> is still a function.</p>
<h2><a class="toc-backref" href="#id40">4.10 Why isn't there a switch or case statement in Python?</a></h2>
<p>You can do this easily enough with a sequence of <tt class="docutils literal"><span class="pre">if...</span> <span class="pre">elif...</span> <span class="pre">elif...</span> <span class="pre">else</span></tt>.
There have been some proposals for switch statement syntax, but there is no
consensus (yet) on whether and how to do range tests. See <a class="reference external" href="../../peps/pep-0275.html">PEP 275</a> for complete details and
the current status.</p>
<p>For cases where you need to choose from a very large number of
possibilities, you can create a dictionary mapping case values to
functions to call. For example:</p>
<pre class="literal-block">
def function_1 (...):
...
functions = {'a': function_1,
'b': function_2,
'c': self.method_1, ...}
func = functions[value]
func()
</pre>
<p>For calling methods on objects, you can simplify yet further by using
the <tt class="docutils literal"><span class="pre">getattr()</span></tt> built-in to retrieve methods with a particular name:</p>
<pre class="literal-block">
def visit_a (self, ...):
...
...
def dispatch (self, value):
method_name = 'visit_' + str(value)
method = getattr(self, method_name)
method()
</pre>
<p>It's suggested that you use a prefix for the method names, such as
<tt class="docutils literal"><span class="pre">visit_</span></tt> in this example. Without such a prefix, if values are coming
from an untrusted source, an attacker would be able to call any method
<h2><a class="toc-backref" href="#id41">4.11 Can't you emulate threads in the interpreter instead of relying on an OS-specific thread implementation?</a></h2>
<p>Answer 1: Unfortunately, the interpreter pushes at least one C stack frame
for each Python stack frame. Also, extensions can call back into Python at
almost random moments. Therefore, a complete threads implementation
requires thread support for C.</p>
<p>Answer 2: Fortunately, there is <a class="reference external" href="http://www.stackless.com">Stackless Python</a>, which has a completely redesigned interpreter
loop that avoids the C stack. It's still experimental but looks very
promising. Although it is binary compatible with standard Python, it's
still unclear whether Stackless will make it into the core -- maybe it's
<h2><a class="toc-backref" href="#id43">4.13 Can Python be compiled to machine code, C or some other language?</a></h2>
<p>Not easily. Python's high level data types, dynamic typing of objects and
run-time invocation of the interpreter (using <tt class="docutils literal"><span class="pre">eval()</span></tt> or <tt class="docutils literal"><span class="pre">exec</span></tt>) together mean
that a "compiled" Python program would probably consist mostly of calls into
the Python run-time system, even for seemingly simple operations like
<h2><a class="toc-backref" href="#id48">4.18 How are dictionaries implemented?</a></h2>
<p>Python's dictionaries are implemented as resizable hash tables.
Compared to B-trees, this gives better performance for lookup
(the most common operation by far) under most circumstances,
and the implementation is simpler.</p>
<p>Dictionaries work by computing a hash code for each key stored in the
dictionary using the <tt class="docutils literal"><span class="pre">hash()</span></tt> built-in function. The hash code
varies widely depending on the key; for example, "Python" hashes to
-539294296 while "python", a string that differs by a single bit,
hashes to 1142331976. The hash code is then used to calculate a
location in an internal array where the value will be stored.
Assuming that you're storing keys that all have different hash values,
this means that dictionaries take constant time -- O(1), in computer
science notation -- to retrieve a key. It also means that no sorted
order of the keys is maintained, and traversing the array as the
<tt class="docutils literal"><span class="pre">.keys()</span></tt> and <tt class="docutils literal"><span class="pre">.items()</span></tt> do will output the dictionary's content
<h2><a class="toc-backref" href="#id52">4.22 Why are default values shared between objects?</a></h2>
<p>This type of bug commonly bites neophyte programmers. Consider this function:</p>
<pre class="literal-block">
def foo(D={}): # Danger: shared reference to one dict for all calls
... compute something ...
D[key] = value
return D
</pre>
<p>The first time you call this function, <tt class="docutils literal"><span class="pre">D</span></tt> contains a single item.
The second time, <tt class="docutils literal"><span class="pre">D</span></tt> contains two items because when <tt class="docutils literal"><span class="pre">foo()</span></tt> begins executing,
<tt class="docutils literal"><span class="pre">D</span></tt> starts out with an item already in it.</p>
<p>It is often expected that a function call creates new objects for
default values. This is not what happens. Default values are created
exactly once, when the function is defined. If that object is
changed, like the dictionary in this example, subsequent calls to the
function will refer to this changed object.</p>
<p>By definition, immutable objects such as numbers, strings, tuples, and
<tt class="docutils literal"><span class="pre">None</span></tt>, are safe from change. Changes to mutable objects such as
dictionaries, lists, and class instances can lead to confusion.</p>
<p>Because of this feature, it is good programming practice to not use mutable
objects as default values. Instead, use <tt class="docutils literal"><span class="pre">None</span></tt> as the default value
and inside the function, check if the parameter is <tt class="docutils literal"><span class="pre">None</span></tt> and create a new list/dictionary/whatever
if it is. For example, don't write:</p>
<pre class="literal-block">
def foo(dict={}):
...
</pre>
<p>but:</p>
<pre class="literal-block">
def foo(dict=None):
if dict is None:
dict = {} # create a new dict for local namespace
</pre>
<p>This feature can be useful. When you have a function that's time-consuming to compute,
a common technique is to cache the parameters and the resulting value of each
call to the function, and return the cached value if the same value is requested again.
This is called "memoizing", and can be implemented like this:</p>
<pre class="literal-block">
# Callers will never provide a third parameter for this function.
def expensive (arg1, arg2, _cache={}):
if _cache.has_key((arg1, arg2)):
return _cache[(arg1, arg2)]
# Calculate the value
result = ... expensive computation ...
_cache[(arg1, arg2)] = result # Store result in the cache
return result
</pre>
<p>You could use a global variable containing a dictionary instead of
the default value; it's a matter of taste.</p>
</div>
<div class="section" id="why-is-there-no-goto">
<h2><a class="toc-backref" href="#id53">4.23 Why is there no goto?</a></h2>
<p>You can use exceptions to provide a "structured goto"
that even works across function calls. Many feel that exceptions
can conveniently emulate all reasonable uses of the "go" or "goto"
constructs of C, Fortran, and other languages. For example:</p>
<pre class="literal-block">
class label: pass # declare a label
try:
...
if (condition): raise label() # goto label
...
except label: # where to goto
pass
...
</pre>
<p>This doesn't allow you to jump into the middle of a loop, but
that's usually considered an abuse of goto anyway. Use sparingly.</p>